from rest_framework.decorators import action
from rest_framework.generics import ListAPIView
from rest_framework.response import Response
from rest_framework.viewsets import ModelViewSet
from users.models import *
from jobs.serializers import *


class EnterpriseModelViewSet(ModelViewSet):
    queryset = Enterprise.objects.all()
    serializer_class = EnterpriseModelSerializer

    @action(methods=['get'], detail=False, url_path='search/hotlist')
    def hotlist(self, request):
        p = self.serializer_class(self.queryset, many=True)
        return Response(p.data)

    @action(methods=['put'], detail=True)
    def visit(self, request, pk):
        data = self.get_object()
        data.visits += 1
        data.save()
        return Response({'message': '更新成功', 'success': True})

    @action(methods=['post'], detail=True)
    def collect(self, request, pk):
        if not request.user.is_anonymous:
            # retruit = self.get_object()
            # users = retruit.users.all()
            enterprise = Enterprise.objects.get(pk=pk)
            users = enterprise.users.all()
            if request.user in users:
                enterprise.users.remove(request.user)
                return Response({'message': '取消收藏成功', 'success': True})
            else:
                enterprise.users.add(request.user)
                return Response({'message': '收藏成功', 'success': True})
        else:
            return Response({'success': False, 'message': '未登录'})

    @action(methods=['post'], detail=True)
    def cancelcollect(self, request, pk):
        if not request.user.is_anonymous:
            # retruit = self.get_object()
            # users = retruit.users.all()
            enterprise = Enterprise.objects.get(pk=pk)
            users = enterprise.users.all()
            if request.user in users:
                enterprise.users.remove(request.user)
                return Response({'message': '取消收藏成功', 'success': True})
            else:
                enterprise.users.add(request.user)
                return Response({'message': '收藏成功', 'success': True})
        else:
            return Response({'success': False, 'message': '未登录'})

    # @action(methods=['post'], detail=True)
    # def collect(self,request,pk):


class RecruitsModelViewSet(ModelViewSet):
    queryset = Recruit.objects.all()
    serializer_class = RecruitModelSerializer

    @action(methods=['get'], detail=False, url_path='search/recommend')
    def recommend(self, request):
        data = self.queryset.order_by('id')[:4]
        p = self.serializer_class(data, many=True)
        return Response(p.data)

    @action(methods=['get'], detail=False, url_path='search/latest')
    def latest(self, request):
        data = self.queryset.order_by('-id')[:4]
        p = self.serializer_class(data, many=True)
        return Response(p.data)

    @action(methods=['post'], detail=True)
    def collect(self, request, pk):
        if not request.user.is_anonymous:
            # retruit = self.get_object()
            # users = retruit.users.all()
            retruit = Recruit.objects.get(pk=pk)
            users = retruit.users.all()
            if request.user in users:
                retruit.users.remove(request.user)
                return Response({'message': '取消收藏成功', 'success': True})
            else:
                retruit.users.add(request.user)
                return Response({'message': '收藏成功', 'success': True})
        else:
            return Response({'success': False, 'message': '未登录'})

    @action(methods=['post'], detail=True)
    def cancelcollect(self, request, pk):
        if not request.user.is_anonymous:
            # retruit = self.get_object()
            # users = retruit.users.all()
            retruit = Recruit.objects.get(pk=pk)
            users = retruit.users.all()
            if request.user in users:
                retruit.users.remove(request.user)
                return Response({'message': '取消收藏成功', 'success': True})
            else:
                retruit.users.add(request.user)
                return Response({'message': '收藏成功', 'success': True})
        else:
            return Response({'success': False, 'message': '未登录'})

    @action(methods=['put'], detail=True)
    def visit(self, request, pk):
        data = self.get_object()
        data.visits += 1
        data.save()
        return Response({'message': '更新成功', 'success': True})

    @action(methods=['post'], detail=False, url_path='search/city/keyword')
    def keyword(self,request):
        dict = request.data
        if len(dict) == 1:
            # 忽略大小写过滤查询
            recruit = Recruit.objects.filter(jobname__icontains=dict['keyword'])
            p = self.serializer_class(recruit, many=True)
            return Response(p.data)
        recruit = Recruit.objects.filter(city=dict['cityname']).filter(jobname__icontains=dict['keyword'])
        p = self.serializer_class(recruit, many=True)
        return Response(p.data)


class CityList(ListAPIView):
    queryset = City.objects.all()
    serializer_class = CityModelSerializer
